﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Acsys.Entity;
using MySql.Data.MySqlClient;
using System.Collections;

namespace Acsys
{
    public class CreditTransactionRepository : Repository
    {
        public CreditTransactionRepository()
            : base()
        { }
        public override void Save(IEntity e)
        {
            try
            {
                CreditTransaction drtr = (CreditTransaction)e;
                OpenConnection();
                MySqlCommand aCommand = new MySqlCommand(drtr.GetInsertSQL(), m_connection);
                e.SetID(aCommand.ExecuteNonQuery());
                aCommand.CommandText = drtr.GetMaximumIDSQL();
                object xx = aCommand.ExecuteScalar();
                int id = xx == null ? 0 : Convert.ToInt32(xx);
                drtr.ID = id;
                foreach (PaymentCreditTransaction pyd in drtr.PAYMENT_LIST)
                {
                    aCommand.CommandText = pyd.GetInsertSQL();
                    aCommand.ExecuteNonQuery();
                }
            }
            catch (Exception x)
            {
                throw new Exception(getErrorMessage(x));
            }
            finally
            {
                m_connection.Close();
            }
        }
        public override void Update(IEntity e)
        {
            try
            {
                OpenConnection();
                CreditTransaction drtr = (CreditTransaction)e;
                MySqlCommand aCommand = new MySqlCommand(e.GetUpdateSQL(), m_connection);
                aCommand.ExecuteNonQuery();
                aCommand.CommandText = PaymentCreditTransaction.GetDeleteByCreditTransactionIDSQL(drtr.ID);
                aCommand.ExecuteNonQuery();
                foreach (PaymentCreditTransaction pyd in drtr.PAYMENT_LIST)
                {
                    aCommand.CommandText = pyd.GetInsertSQL();
                    aCommand.ExecuteNonQuery();
                }
            }
            catch (Exception x)
            {
                throw new Exception(getErrorMessage(x));
            }
            finally
            {
                m_connection.Close();
            }
        }
        public IList GetPaymentList(int drtrID)
        {
            try
            {
                OpenConnection();
                PaymentCreditTransaction e = new PaymentCreditTransaction();
                MySqlCommand aCommand = new MySqlCommand(PaymentCreditTransaction.GetByCreditTransactionIDSQL(drtrID), m_connection);
                MySqlDataReader aReader = aCommand.ExecuteReader();
                IList a = e.GetAll(aReader);
                return a;
            }
            catch (Exception x)
            {
                throw new Exception(getErrorMessage(x));
            }
            finally
            {
                m_connection.Close();
            }
        }
        public IList SearchBy(string text)
        {
            try
            {
                OpenConnection();
                CreditTransaction e = new CreditTransaction();
                MySqlCommand aCommand = new MySqlCommand(text.Trim() == "" ? e.GetAllSQL() : e.GetSearchSQL(text), m_connection);
                MySqlDataReader aReader = aCommand.ExecuteReader();
                IList a = e.GetAll(aReader);
                return a;
            }
            catch (Exception x)
            {
                throw new Exception(getErrorMessage(x));
            }
            finally
            {
                m_connection.Close();
            }

        }
        public override void Delete(IEntity e)
        {
            try
            {
                IList pays = this.GetPaymentList(e.GetID());
                OpenConnection();
                MySqlCommand aCommand = new MySqlCommand();
                aCommand.Connection = m_connection;
                foreach (PaymentCreditTransaction t in pays)
                {
                    aCommand.CommandText = t.GetDeleteSQL();
                    aCommand.ExecuteNonQuery();
                }
                aCommand.CommandText = e.GetDeleteSQL();
                aCommand.ExecuteNonQuery();
            }
            catch (Exception x)
            {
                throw new Exception(getErrorMessage(x));
            }
            finally
            {
                m_connection.Close();
            }
        }
    }
}
